package com.cn.yunhe.struts.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cn.yunhe.mybatis.model.Page;
import com.cn.yunhe.mybatis.model.UserModel;
import com.cn.yunhe.mybatis.service.IUserService;
import com.cn.yunhe.spring.test.TestSpringMybaits3;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class UserController extends ActionSupport implements ModelDriven<Object> {
	private static Logger log = LogManager.getLogger(UserController.class);

	private static final long serialVersionUID = 1L;
	// 因为extends
	// ActionSupport(可以获取值通过getter方法)，所以这里可以UserModer等等为属性值。其他都不可以，必须通过new来获取
	@Autowired
	private IUserService userService;
	private UserModel user;
	private int pageSize;
	private int pageNum;
	private Page page;

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public UserModel getUser() {
		return user;
	}

	public void setUser(UserModel user) {
		this.user = user;
	}

	public IUserService getUserService() {
		return userService;
	}

	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	/**
	 * 猪唠....记得这个一定不能返回null
	 */
	public Object getModel() {
		// TODO Auto-generated method stub
		if (user == null) {
			user = new UserModel();
		}
		return user;
	}
//查找
	public String findUser() {
		user = userService.findUser(user);
		ServletActionContext.getRequest().setAttribute("data", user);
		log.info("查找用户");
		return "success";

	}

	// 登陆
	public String getUserByName() {
		log.info("登陆用户");
		try {
			PrintWriter out = ServletActionContext.getResponse().getWriter();
			JSONObject jo = new JSONObject();
			user = userService.getUser(user);
			if (user!= null) {
				jo.put("rsCode", 1);
				jo.put("rsMsg", "list.html");
				out.print(JSON.toJSONString(jo));
			} else {
				jo.put("rsCode", 2);
				jo.put("rsMsg", "login.html");
				out.print(JSON.toJSONString(jo));
			}
			out.flush();
			out.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

		return "success";
	}
//添加
	public String addUser() {
		userService.insert(user);
		log.info("添加用户");
		return "success";
	}

	public String delUser() {
		userService.delete(user);
		log.info("删除用户");
		return "success";
	}

	public String updateUser() {
		userService.update(user);
		log.info("修改用户");
		return "success";
	}
//查找，分页
	public String getUserList() {
		Map<String, Object> pList = new HashMap<String, Object>();
		pList.put("pageSize", pageSize);
		pList.put("pageNum", pageNum);
		pList.put("user", user);
		page = userService.getUserList(pList);
		log.info("显示列表");
		return "success";

	}
}